﻿var permissionListTableVue = new Vue({
    el: "#permissionListTable",
    data: {
        hasNewPermission: false,      //判断是否正在新建权限
        hasEditPermission: false,     //判断是否正在编辑权限
        permissionList: [],
        isDisableDictionary: {}
    },
    methods: {
        AddPermission: function () {
            AddPermissionDetail(this);
        },
        AddSubPermission: function (currentPermission) {
            AddSubPermissionDetail(this, currentPermission);
        },
        SavePermission: function (currentPermissionInfo) {
            SavePermissionDetail(this, currentPermissionInfo);
        },
        CancelSavePermission: function (currentPermissionInfo) {
            CancelSavePermissionDetail(this, currentPermissionInfo);
        },
        EditPermission: function (currentPermissionInfo) {
            EditPermissionDetail(this, currentPermissionInfo);
        },
        UpdatePermission: function (currentPermissionInfo) {
            UpdatePermissionDetail(this, currentPermissionInfo);
        },
        CancelUpdatePermission: function (currentPermissionInfo) {
            CancelUpdatePermissionDetail(this, currentPermissionInfo);
        },
        DeletePermission: function (currentPermissionInfo) {
            DeletePermissionDetail(this, currentPermissionInfo);
        }
    },
    created: function () {
        //页面加载前初始的数据
        InitialAztechScript();
    },
    mounted: function () {
        //页面加载后初始的数据
        this.isDisableDictionary = GlobalDictionary.GetTrueOrFalseDictionary();
        ListPermission(this);
    }
});

/**
 * 查询所有权限信息
 */
function ListPermission(vue) {
    ShowAzLoading();
    $.ajax({
        type: "GET",
        url: "/Account/ListPermission?v=" + Math.random(),
        success: function (res) {
            if (res.status == "Success") {
                vue.permissionList = res.data;

                //禁用所有Input和Select标签
                vue.$nextTick(function () {
                    $("input").prop("disabled", true);
                    $("select").prop("disabled", true);
                });
            } else {
                toastr.error(res.message);
            }
            HideAzLoading();
        },
        error: function (err) {
            console.log(err);
            toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
            HideAzLoading();
        }
    })
}

/**
 * 创建指定ParentID的空权限
 */
function CreatePermission(parentID) {
    var permission = {
        ID: 0,
        Name: "",
        UrlPath: "",
        ParentID: parentID,
        Priority: 0,
        IsDisable: 0
    };
    return permission;
}

/**
 * 增加权限
 */
function AddPermissionDetail(vue) {
    if (vue.hasEditPermission || vue.hasNewPermission) {
        toastr.error("请保存正在编辑的权限后再新增权限Please save the permission being edited before adding new permission。");
    } else {
        vue.permissionList.push({ BaseInfo: CreatePermission(0) });
        vue.hasNewPermission = true;
    }
}

/**
 * 增加子权限
 */
function AddSubPermissionDetail(vue, permission) {
    if (vue.hasEditPermission || vue.hasNewPermission) {
        toastr.error("请保存正在编辑的权限后再新增权限Please save the permission being edited before adding new permission。");
    } else {
        if (!permission.SubPermissions)
            permission.SubPermissions = [];

        permission.SubPermissions.push(CreatePermission(permission.BaseInfo.ID));
        vue.hasNewPermission = true;
    }
}

/**
 * 保存新增权限
 */
function SavePermissionDetail(vue, permissionInfo) {
    if (CheckPermission(permissionInfo)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/SavePermission",
            data: { permission: permissionInfo },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("保存权限信息成功Permission information saved successfully。");
                    vue.hasNewPermission = false;
                    ListPermission(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消新增权限
 */
function CancelSavePermissionDetail(vue, permissionInfo) {
    ListPermission(vue);
    vue.hasNewPermission = false;
}

/**
 * 编辑权限
 */
function EditPermissionDetail(vue, permissionInfo) {
    if (vue.hasNewPermission || vue.hasEditPermission) {
        toastr.error("请保存正在编辑的权限后再新增权限Please save the permission being edited before adding new permission。");
    } else {
        ShowEditPermissionButton(vue, permissionInfo.ID);
    }

}

/**
 * 更新编辑中权限
 */
function UpdatePermissionDetail(vue, permissionInfo) {
    if (CheckPermission(permissionInfo)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/UpdatePermission",
            data: { permission: permissionInfo },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("更新权限信息成功Permission information updated successfully。");
                    HideEditPermissionButton(vue, permissionInfo.ID);
                    ListPermission(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 取消编辑中权限
 */
function CancelUpdatePermissionDetail(vue, permissionInfo) {
    HideEditPermissionButton(vue, permissionInfo.ID);
}

/**
 * 删除编辑中权限
 */
function DeletePermissionDetail(vue, permissionInfo) {
    var message = "确认删除权限Confirm delete permission[" + permissionInfo.Name + "]?";
    if (confirm(message)) {
        ShowAzLoading();
        $.ajax({
            type: "POST",
            url: "/Account/DeletePermission",
            data: { PermissionID: permissionInfo.ID },
            success: function (res) {
                if (res.status == "Success") {
                    toastr.success("删除权限信息成功Permission information deleted successfully。");
                    HideEditPermissionButton(vue, permissionInfo.ID);
                    ListPermission(vue);
                } else {
                    toastr.error(res.message);
                }
                HideAzLoading();
            },
            error: function (err) {
                console.log(err);
                toastr.error("服务器发生错误，请稍候再试An error occurred in the server. Please try again later。");
                HideAzLoading();
            }
        });
    }
}

/**
 * 显示编辑权限按钮
 */
function ShowEditPermissionButton(vue, permissionID) {
    $("#inputArea" + permissionID).find("input").prop("disabled", false);
    $("#btnEdit" + permissionID).hide();
    $("#btnUpdate" + permissionID).show();
    $("#btnCancelUpdate" + permissionID).show();
    $("#btnDelete" + permissionID).show();
    vue.hasEditPermission = true;
}

/**
 * 隐藏编辑权限按钮
 */
function HideEditPermissionButton(vue, permissionID) {
    $("#inputArea" + permissionID).find("input").prop("disabled", true);
    $("#btnEdit" + permissionID).show();
    $("#btnUpdate" + permissionID).hide();
    $("#btnCancelUpdate" + permissionID).hide();
    $("#btnDelete" + permissionID).hide();
    vue.hasEditPermission = false;
}

/**
 * 检查权限内容正确性
 */
function CheckPermission(permissionInfo) {
    if (!CheckPermissionName(permissionInfo))
        return false;
    if (!CheckPriority(permissionInfo))
        return false;
    return true;
}

/**
 * 检查权限名字
 */
function CheckPermissionName(permissionInfo) {
    $("#inputName" + permissionInfo.ID).addClass("has-error");
    if (permissionInfo.Name != null && permissionInfo.Name.length > 0) {
        $("#inputName" + permissionInfo.ID).removeClass("has-error");
        return true;
    }
    return false;
}

/**
 * 检查优先级内容
 */
function CheckPriority(permissionInfo) {
    var reg = /^[0-9]+$/;
    var result = reg.test(permissionInfo.Priority);

    $("#inputPriority" + permissionInfo.ID).addClass("has-error");
    if (result) {
        $("#inputPriority" + permissionInfo.ID).removeClass("has-error");
        return true;
    }
    return false;
}
